package com.jdbc;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * DAO 基类
 *` Created by CCX on 2018/7/8.
 */
public class DAOBaseImpl<T> implements DAOOperationInterface {
    private QueryRunner queryRunner = null;
    private Class<T> type;
    public DAOBaseImpl() {
        queryRunner = new QueryRunner();
        type = ReflectionUtils.getSuperGenericType(getClass());
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        return super.equals(obj);
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public String toString() {
        return super.toString();
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
    }


    public void batch(Connection connection, String sql, Object[]... args) throws SQLException {
        queryRunner.update(connection,sql,args);
    }

    public List getForList(Connection connection, String sql, Object... args) throws SQLException {
        return  queryRunner.query(connection, sql,
                new BeanListHandler<T>(type), args);
    }
    public T get(Connection connection, String sql, Object... args) throws SQLException {
        return  queryRunner.query(connection, sql,
                new BeanHandler<T>(type), args);
    }

    public void update(Connection connection, String sql, Object... args) throws SQLException {
        queryRunner.update(connection,sql,args);
    }

    public T getForValue(Connection connection, String sql, Object... args) throws SQLException {
        return queryRunner.query(connection, sql,
                new BeanHandler<T>(type), args);
    }
}
